﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic;
using Module.Core.BusinessLogic.CmsContents;
using Module.Common.Models;
using Module.Core.Common;
using Module.Core.EntitiesInfo;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.Utilities;

namespace Web.Cms.Content.Zones
{
    public partial class ZoneManager : AdminUserControl
    {
        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    CHRTList<CMSZonesInfo> listZone = CMSZonesBL.GetParentZones(0, Const.VN, 0, 0);
                    grdZone.DataSource = CMSZonesBL.FillToList(0, Const.VN, 0, 0);
                    grdZone.RowDataBound += new GridViewRowEventHandler(grdZone_RowDataBound);
                    grdZone.DataBind();

                    dropParent.DataSource = listZone;
                    dropParent.DataTextField = "Title";
                    dropParent.DataValueField = "ZoneIDLang";
                    dropParent.DataBind();
                    dropParent.Items.Insert(0, new ListItem("---------Xem tất cả--------", "0"));

                    grdZone.DataSource = CMSZonesBL.FillToList(Convert.ToInt32(dropParent.SelectedValue), Const.VN, 0, 0);
                    grdZone.DataBind();
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
        }
        #endregion

        #region RowDataBound
        public void grdZone_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DataRowView drv = (DataRowView)e.Row.DataItem;
                    DropDownList dropPriority = (DropDownList)e.Row.FindControl("dropPriority");
                    ListPriority.ByDropList(dropPriority, 50);
                    ObjMiscUtility.SetSelected(dropPriority, drv["Priority"].ToString());
                    e.Row.Cells[0].Text = Convert.ToString(e.Row.DataItemIndex + 1);
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
        }
        #endregion

        #region RowCommand
        protected void grdZone_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "SetPriority":
                        {
                            /*
                            foreach (GridViewRow _ZoneInfo in grdZone.Rows)
                            {
                                if (_ZoneInfo.RowType == DataControlRowType.DataRow)
                                {

                                    DropDownList dropPriority = (DropDownList)_ZoneInfo.FindControl("dropPriority");                                

                                }
                            }
                              */
                            break;

                        }
                    case "Commanddelete":
                        {
                            int parentId = 0;
                            int zoneId = Convert.ToInt32(e.CommandArgument);
                            CMSZonesInfo info = CMSZonesBL.Select(zoneId);
                            if (info != null)
                            {
                                parentId = info.ParentID;
                            }
                            CMSZonesBL.Delete(zoneId);                            
                            Response.Redirect(ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey, new string[] { }, new string[] { }));
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }

        }
        #endregion

        #region SelectedIndexChange
        protected void dropParent_SelectedIndexChanged(object sender, EventArgs e)
        {
            grdZone.DataSource = CMSZonesBL.FillToList(Convert.ToInt32(dropParent.SelectedValue), Const.VN, 0, 0);
            grdZone.DataBind();
        }
        #endregion
    }
}